package com.zeng.sim.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.IDbQuery;
import com.baomidou.mybatisplus.generator.config.IKeyWordsHandler;
import com.baomidou.mybatisplus.generator.config.ITypeConvert;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.SqliteTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.SqliteQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.query.SQLQuery;
import org.apache.ibatis.annotations.Mapper;
import org.junit.jupiter.api.Test;
import org.sqlite.JDBC;

import java.io.File;
import java.util.Collections;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-09-13T8:59
 */
public class MybatisPlusGeneratorRunner {

    @Test
    public void generate() throws Exception {
        // 数据库信息
        final String url = "jdbc:sqlite::resource:db/sim.db";
        final String schema = null;
        final String username = "";
        final String password = "";
        // 要生成的表名
        final String[] tableNameArr = new String[] { "sim_msg_record" };
        // 项目路径
        final String projectPath = new File("").getCanonicalPath();
        final String author = "zengwanlin";

        // 先加载驱动
        JDBC.class.newInstance();

        DataSourceConfig dataSourceConfig;
        IDbQuery dbQuery = new SqliteQuery();
        ITypeConvert typeConvert = new SqliteTypeConvert();
        IKeyWordsHandler keyWordsHandler = null;// new MySqlKeyWordsHandler()
        // 使用SQL查询的方式生成代码,属于旧的代码生成方式,通用性不是好,老的代码可以继续使用,适配数据库需要完成dbQuery和typeConvert的扩展,后期不再维护这种方式
        DataSourceConfig.Builder dataSourceConfigBuilder;
        dataSourceConfigBuilder = new DataSourceConfig.Builder(url, username, password).dbQuery(dbQuery)
            .schema(schema)
            .typeConvert(typeConvert)
            .keyWordsHandler(keyWordsHandler)
            .databaseQueryClass(SQLQuery.class);

        // 使用元数据查询的方式生成代码,默认已经根据jdbcType来适配java类型,支持使用typeConvertHandler来转换需要映射的类型映射
        // dataSourceConfigBuilder = new DataSourceConfig.Builder(url, username, password)
        // .dbQuery(dbQuery)
        // .schema(schema)
        // .typeConvert(typeConvert)
        // .keyWordsHandler(keyWordsHandler);

        FastAutoGenerator.create(dataSourceConfigBuilder).globalConfig(builder -> {
            builder.author(author)
                .disableOpenDir()
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
                .outputDir(projectPath + "/src/main/java");
        }).packageConfig(builder -> {
            builder.parent("com.zeng.sim")
                .mapper("mapper")
                .entity("mapper.model")
                .xml("Mapper.xml")
                .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper"));
        }).templateConfig(builder -> {
            builder.controller(null).service(null).serviceImpl(null);
        }).strategyConfig(builder -> {
            builder.addInclude(tableNameArr)
                .enableCapitalMode()
                .disableSqlFilter()

                .entityBuilder()
                .enableFileOverride()
                .enableLombok()
                .idType(IdType.AUTO)
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                // .addSuperEntityColumns("ID", "CREATE_BY", "UPDATE_BY", "CREATE_TIME",
                // "UPDATE_TIME")
                .addTableFills(new Property("createTime", FieldFill.INSERT))
                .addTableFills(new Property("updateTime", FieldFill.UPDATE))
                .formatFileName("%sEntity")

                .controllerBuilder()
                .enableFileOverride()
                .formatFileName("%sController")

                .serviceBuilder()
                .enableFileOverride()
                .formatServiceFileName("%sService")
                .formatServiceImplFileName("%sServiceImpl")

                .mapperBuilder()
                .enableFileOverride()
                .mapperAnnotation(Mapper.class)
                .enableBaseResultMap()
                .enableBaseColumnList()
                .formatMapperFileName("%sMapper")
                .formatXmlFileName("%sMapper");
        }).templateEngine(new FreemarkerTemplateEngine()).execute();
    }

}
